package com.app.mymusic.ui.login;

import com.app.mymusic.common.Constants;
import com.app.mymusic.utils.LogUtil;
import com.app.mymusic.utils.RetrofitUtil;

import java.io.IOException;

import okhttp3.ResponseBody;
import rx.Subscriber;

/**
 * Created by Administrator on 2016-12-6.
 */

public class LoginModel {

    public LoginModel() {
    }

    public void login (String name , String psw, String id, final LoginPresenter.LoginCallback callback){
         RetrofitUtil.getInstace()
                     .initRetrofit(Constants.getUrl())
                     .login(name, psw, id, new Subscriber<ResponseBody>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                LogUtil.showLog("Throwable:"+e);
                callback.loginError(e.toString());
            }

            @Override
            public void onNext(ResponseBody responseBody) {
                try {
                    String res=responseBody.string();
                    callback.loginSuccess(res);
                    LogUtil.showLog("res:"+res);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
